package org.dromara.business.modules.favorites.controller;

import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.modules.favorites.domain.bo.BuFavoritesBo;
import org.dromara.business.modules.favorites.domain.vo.BuFavoritesVo;
import org.dromara.business.modules.favorites.domain.vo.MyCountVo;
import org.dromara.business.modules.favorites.service.IBuFavoritesService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收藏
 *
 * @author ckk
 * @date 2025-11-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/favorites")
public class BuFavoritesController extends BaseController {

    private final IBuFavoritesService buFavoritesService;

    /**
     * 我的-查询数量
     */
    @GetMapping("/myCount")
    public R<MyCountVo> myCount() {
        return R.ok(buFavoritesService.myCount());
    }

    /**
     * 查询用户收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo<BuFavoritesVo> list(BuFavoritesBo bo, PageQuery pageQuery) {
        return buFavoritesService.queryPageList(bo, pageQuery);
    }

    /**
     * 收藏
     */
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BuFavoritesBo bo) {
        return toAjax(buFavoritesService.insertByBo(bo));
    }

    /**
     * 取消收藏
     */
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> cancel(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        buFavoritesService.cancel(id);
        return R.ok();
    }

    /**
     * 是否收藏
     */
    @GetMapping("/isFavorites/{id}")
    public R<Boolean> isFavorites(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(buFavoritesService.isFavorites(id));
    }
}
